#'Converts to 2010 tracts.
#'
#'@param df dataframe. Dataframe with tract column to be converted.
#'@param conversion_filepath character. Filepath to tract conversion
#'information.
#'@param old_tract_col character. Name of tract column in tract
#'conversion file to be converted.
#'@param new_tract_col character. Name of tract column to convert to
#' in tract conversion file.
#'@param join_col character. Name of ID column in df to join
#'on with conversion file.
#'@param cols_to_summarize character vector. Names of numeric columns
#'in df to re-weight according to the tract conversion weights.
#'
#'@return dataframe with new tract, year and weighted sum of
#'cols_to_summarize using conversion weight.
#'@export
.convert_to_2010_tracts <- function(df,
                                   conversion_filepath,
                                   old_tract_col,
                                   new_tract_col,
                                   join_col=NULL,
                                   cols_to_summarize=NULL) {

  ltd <- read.csv(conversion_filepath) %>%
    select(old_tract=!!sym(old_tract_col), !!sym(new_tract_col), weight) %>%
    mutate(!!sym(new_tract_col) := str_pad(!!sym(new_tract_col), 11, side="left", pad="0"),
           old_tract = str_pad(old_tract, 11, side="left", pad="0"))

  if (!is.null(cols_to_summarize)) {

    df <- df %>%
      inner_join(ltd, by=setNames("old_tract", join_col)) %>%
      group_by(!!sym(new_tract_col), year)

    for (col in cols_to_summarize) {

      df <- df %>%
        mutate(!!sym(col) := sum(!!sym(col)*weight, na.rm=T))

    }

    df <- df %>%
      ungroup() %>%
      select(!!sym(new_tract_col), year, all_of(cols_to_summarize)) %>%
      distinct()

    return(df)

  } else {

    return(ltd)

  }
}
